package com.yungu.swift.report.controller;

import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.admin.AdminReportParam;
import com.yungu.swift.report.entity.ReportOrderAllocateDay;
import com.yungu.swift.report.service.OrderAllocateDayService;
import com.yungu.swift.report.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 订单效率统计控制器：派单成功率和派单时长
 */
@Slf4j
@RestController
@Api(value = "【订单效率】", tags = "订单效率")
public class OrderAllocateDayCtrl {
    @Autowired
    private OrderAllocateDayService orderAllocateDayService;

    @PostMapping("/report/allocateDay/total")
    @ApiOperation(value = "订单效率总量", httpMethod = "POST")
    public ResponseData<ReportOrderAllocateDay> getTotal(@RequestBody AdminReportParam reportParam) {
        return orderAllocateDayService.getTotal(reportParam);
    }

    @PostMapping("/report/allocateDay/queryPage")
    @ApiOperation(value = "订单效率分页查询", httpMethod = "POST")
    public ResponseData<Map<String, Object>> queryPage(@RequestBody AdminReportParam reportParam) {
        return orderAllocateDayService.queryPage(reportParam);
    }


    @PostMapping("/report/allocateDay/export")
    @ApiOperation(value = "订单效率分页查询导出", httpMethod = "POST")
    public void export(@RequestBody AdminReportParam reportParam, HttpServletResponse response) throws Exception {
        List list = this.orderAllocateDayService.list(reportParam).getData();
        String templatePath = "/template/order_allocate_rate_day_report.xlsx";
        String description = "派单成功率";
        if (reportParam.getTypeReport() == 2) {
            templatePath = "/template/order_allocate_avg_duration_day_report.xlsx";
            description = "派单等待时长";
        }
        if (CollectionUtils.isEmpty(list)) {
            log.warn("{} 报表,没有统计数据可导出", description);

        }
        ExcelUtil.download(reportParam, response, list, ReportOrderAllocateDay.class, templatePath, description);
    }
}
